/*
 * Copyright 2006-2009 Wolfgang Gehner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mycompany.strutsmodule1;

import java.util.List;


import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.googlecode.scopeplugin.ScopeType;
import com.googlecode.scopeplugin.annotations.In;
import com.googlecode.scopeplugin.annotations.Out;
import com.opensymphony.xwork2.ActionSupport;

public class Module1Action extends ActionSupport implements ServletRequestAware {

	private static final long serialVersionUID = 1L;
	
	@In (scope=ScopeType.SESSION)
	@Out (scope=ScopeType.SESSION)
	List<String> resultList = null;
	
	
	private HttpServletRequest req;
	
	public List<String> getResultList() {
		return resultList;
	}
	
	public String execute() throws Exception {

		if ("sell".equals(req.getParameter("method")))
			return sell();
		
		if (resultList == null) //create portfolio
		{	
			create(); 
		}
		
		return SUCCESS;
	}
	
	public String sell() throws Exception {

		int id = new Integer(req.getParameter("id")).intValue()-1;
		
		resultList.remove(id);
		
		req.getSession(true).setAttribute("message", "Successfully sold position for "+new Module1BusinessObject().myCommonCalculation(true));
		
		return "home";
	}
	
	public String liquidate() throws Exception {

		resultList.removeAll(resultList);
		
		req.getSession(true).setAttribute("message", "Successfully liquidated portfolio for "+new Module1BusinessObject().myCommonCalculation(false));
		
		return "home";
	}
	
	public String create() throws Exception {
		
		Module1BusinessObject obj = new Module1BusinessObject();
		resultList = obj.myBusinessMethod();
		
		return "home";
	}
	
	
	public void setServletRequest(HttpServletRequest req) {
		this.req = req;
	}

}
